﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.Text;
using System.Threading.Tasks;
using Abp;
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Configuration;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using LeadThing.AbpZeroTemplate.VideoReceiverses.Authorization;
using LeadThing.AbpZeroTemplate.VideoReceiverses.Dtos;
using LeadThing.AbpZeroTemplate.Dto;

namespace LeadThing.AbpZeroTemplate.VideoReceiverses
{
    /// <summary>
    /// 视频接访服务实现
    /// </summary>
    [AbpAuthorize(VideoReceiversAppPermissions.VideoReceivers)]


    public class VideoReceiversAppService : AbpZeroTemplateAppServiceBase, IVideoReceiversAppService
    {
        private readonly IRepository<VideoReceivers, long> _videoReceiversRepository;
        private readonly IVideoReceiversListExcelExporter _videoReceiversListExcelExporter;


        private readonly VideoReceiversManage _videoReceiversManage;
        /// <summary>
        /// 构造方法
        /// </summary>
        public VideoReceiversAppService(IRepository<VideoReceivers, long> videoReceiversRepository,
VideoReceiversManage videoReceiversManage
      , IVideoReceiversListExcelExporter videoReceiversListExcelExporter
  )
        {
            _videoReceiversRepository = videoReceiversRepository;
            _videoReceiversManage = videoReceiversManage;
            _videoReceiversListExcelExporter = videoReceiversListExcelExporter;
        }


        #region 实体的自定义扩展方法
        private IQueryable<VideoReceivers> _videoReceiversRepositoryAsNoTrack => _videoReceiversRepository.GetAll().AsNoTracking();


        #endregion


        #region 视频接访管理

        /// <summary>
        /// 根据查询条件获取视频接访分页列表
        /// </summary>
        public async Task<PagedResultDto<VideoReceiversListDto>> GetPagedVideoReceiverssAsync(GetVideoReceiversInput input)
        {

            var query = _videoReceiversRepositoryAsNoTrack;
            //TODO:根据传入的参数添加过滤条件
            if (!string.IsNullOrWhiteSpace(input.Filter))
            {
                query = query.Where(p => p.Title.Contains(input.Filter));
            }

            var videoReceiversCount = await query.CountAsync();

            var videoReceiverss = await query
            .OrderByDescending(p => p.Id)
            .PageBy(input)
            .ToListAsync();

            var videoReceiversListDtos = videoReceiverss.MapTo<List<VideoReceiversListDto>>();
            return new PagedResultDto<VideoReceiversListDto>(
            videoReceiversCount,
            videoReceiversListDtos
            );
        }

        /// <summary>
        /// 通过Id获取视频接访信息进行编辑或修改 
        /// </summary>
        public async Task<GetVideoReceiversForEditOutput> GetVideoReceiversForEditAsync(NullableIdDto<long> input)
        {
            var output = new GetVideoReceiversForEditOutput();

            VideoReceiversEditDto videoReceiversEditDto;

            if (input.Id.HasValue)
            {
                var entity = await _videoReceiversRepository.GetAsync(input.Id.Value);
                videoReceiversEditDto = entity.MapTo<VideoReceiversEditDto>();
            }
            else
            {
                videoReceiversEditDto = new VideoReceiversEditDto();
            }

            output.VideoReceivers = videoReceiversEditDto;
            return output;
        }


        /// <summary>
        /// 通过指定id获取视频接访ListDto信息
        /// </summary>
        public async Task<VideoReceiversListDto> GetVideoReceiversByIdAsync(EntityDto<long> input)
        {
            var entity = await _videoReceiversRepository.GetAsync(input.Id);

            return entity.MapTo<VideoReceiversListDto>();
        }







        /// <summary>
        /// 新增或更改视频接访
        /// </summary>
        public async Task CreateOrUpdateVideoReceiversAsync(CreateOrUpdateVideoReceiversInput input)
        {
            if (input.VideoReceiversEditDto.Id.HasValue)
            {
                await UpdateVideoReceiversAsync(input.VideoReceiversEditDto);
            }
            else
            {
                await CreateVideoReceiversAsync(input.VideoReceiversEditDto);
            }
        }

        /// <summary>
        /// 新增视频接访
        /// </summary>
        [AbpAuthorize(VideoReceiversAppPermissions.VideoReceivers_CreateVideoReceivers)]
        public virtual async Task<VideoReceiversEditDto> CreateVideoReceiversAsync(VideoReceiversEditDto input)
        {
            //TODO:新增前的逻辑判断，是否允许新增

            var entity = input.MapTo<VideoReceivers>();

            entity = await _videoReceiversRepository.InsertAsync(entity);
            return entity.MapTo<VideoReceiversEditDto>();
        }

        /// <summary>
        /// 编辑视频接访
        /// </summary>
        [AbpAuthorize(VideoReceiversAppPermissions.VideoReceivers_EditVideoReceivers)]
        public virtual async Task UpdateVideoReceiversAsync(VideoReceiversEditDto input)
        {
            //TODO:更新前的逻辑判断，是否允许更新

            var entity = await _videoReceiversRepository.GetAsync(input.Id.Value);
            input.MapTo(entity);

            await _videoReceiversRepository.UpdateAsync(entity);
        }

        /// <summary>
        /// 删除视频接访
        /// </summary>
        [AbpAuthorize(VideoReceiversAppPermissions.VideoReceivers_DeleteVideoReceivers)]
        public async Task DeleteVideoReceiversAsync(EntityDto<long> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _videoReceiversRepository.DeleteAsync(input.Id);
        }

        /// <summary>
        /// 批量删除视频接访
        /// </summary>
        [AbpAuthorize(VideoReceiversAppPermissions.VideoReceivers_DeleteVideoReceivers)]
        public async Task BatchDeleteVideoReceiversAsync(List<long> input)
        {
            //TODO:批量删除前的逻辑判断，是否允许删除
            await _videoReceiversRepository.DeleteAsync(s => input.Contains(s.Id));
        }

        #endregion

        #region 视频接访的Excel导出功能


        public async Task<FileDto> GetVideoReceiversToExcel()
        {
            var entities = await _videoReceiversRepository.GetAll().ToListAsync();

            var dtos = entities.MapTo<List<VideoReceiversListDto>>();

            var fileDto = _videoReceiversListExcelExporter.ExportVideoReceiversToFile(dtos);



            return fileDto;
        }


        #endregion

    }
}
